package cn.edu.scau.cmi.wuzewei.comprehensive;

import cn.edu.scau.cmi.wuzewei.singleton.Chairman;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class SingletonWindow {
	public void newWindow(Stage primaryStage) {

		Chairman chairman = Chairman.getInstance("毛泽东");

		//创建GridPane容器
		GridPane grid = new GridPane();
		grid.setPadding(new Insets(10, 10, 10, 10));
		grid.setVgap(5);
		grid.setHgap(5);
		
		Label label1 = new Label("主席： " + chairman.getName());
		GridPane.setConstraints(label1, 0, 1);
		grid.getChildren().add(label1);
		
		Label label2 = new Label("对象： " + chairman);
		GridPane.setConstraints(label2, 1, 1);
		grid.getChildren().add(label2);
		
		Label label3 = new Label("新主席： ");
		GridPane.setConstraints(label3, 0, 2);
		grid.getChildren().add(label3);
		
		Label label4 = new Label();
		GridPane.setConstraints(label4, 1, 2);
		grid.getChildren().add(label4);
		
		final TextField name = new TextField();
		name.setPromptText("请输入姓名");
		GridPane.setConstraints(name, 0, 0);
		grid.getChildren().add(name);
		 
		Button submit = new Button("创建主席");
		submit.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				Chairman chairman1 = Chairman.getInstance(name.getText());
				label3.setText("新主席： " + chairman1.getName());
				label4.setText("对象： " + chairman1);
			}
		});
		GridPane.setConstraints(submit, 1, 0);
		grid.getChildren().add(submit);
		
		Scene scene = new Scene(grid);
		primaryStage.setTitle("单例模式");
		primaryStage.setScene(scene);
		primaryStage.show();
	}
}
